﻿using AutoMapper;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.InventoryCommands
{
    /// <summary>
    /// 种母数据
    /// </summary>
    public class SeedMotherRetentionBarrierHandler:IRequestHandler<SeedMotherRetentionBarrierCommon,Result>
    {
        private readonly IBaseRepository<SleepInfor> sleepInforRep;
        public SeedMotherRetentionBarrierHandler(IBaseRepository<SleepInfor> _sleepInforRep)
        {
            sleepInforRep = _sleepInforRep;
        }

        public async Task<Result> Handle(SeedMotherRetentionBarrierCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.success};
            try
            {
                var list  = sleepInforRep.GetAll().Where(c=>c.Gender==2&&c.BreedStatuId==6 || c.BreedStatuId == 7);
                var list2 = sleepInforRep.GetAll().Where(c => c.Gender == 2 && c.BreedStatuId != 6 && c.BreedStatuId != 7);
                var groupedResult = list.GroupBy(x => x.BreedStatuId)
                     .Select(g => new
                     {
                         type = g.Key,
                         countss = g.Count()
                     }).ToList();
                 if(groupedResult.Count>0)
                {
                    result.Code = list2.Count();
                    result.Data = groupedResult;
                }
                else
                {
                    result.Messege = "查询失败";
                }
            }
            catch (Exception)
            {
                result.Messege = "查询失败";
                throw;
            }
            return result; 
        }
    }
}
